package com.zttek.photosShareProject.modules.service.share.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zttek.photosShareProject.common.myException.DBException;
import com.zttek.photosShareProject.modules.mapper.ExamineMapper;
import com.zttek.photosShareProject.modules.model.Examine;
import com.zttek.photosShareProject.modules.service.share.IAbuseService;
import com.zttek.photosShareProject.modules.service.share.IExamineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ExamineService implements IExamineService{

    @Autowired
    private ExamineMapper examineMapper;

    @Autowired
    private IAbuseService abuseService;

    @Transactional
    @Override
    public void queueShare(long targetId, Date date) throws Exception {

        Examine examine = fill(targetId, date);
        examine.setType(SHARE);

//        examineInsert(examine);
        examineMapper.insert(examine);
    }

    @Transactional
    @Override
    public void queueAbuse(long targetId, Date date) throws Exception {

        Examine examine = fill(targetId, date);
        examine.setType(ABUSE);

//        examineInsert(examine);
        examineMapper.insert(examine);
    }

    @Override
    public Page<Examine> selectExamine(Page<Examine> page) throws Exception {
        EntityWrapper<Examine> ew = new EntityWrapper<>();
        ew.where("valid=1").and("status=0");
//        List<Examine> list = examineSelectPage(page, ew);
        List<Examine> list = examineMapper.selectPage(page, ew);
        page.setRecords(list);
        return page;
    }

    @Transactional
    @Override
    public void handle(Long examineId, Integer adminId, boolean isPassed) throws Exception {
//        Examine examine = examineSelectById(examineId);
        Examine examine = examineMapper.selectById(examineId);
        int status = 3;
        if (!isPassed)
            status = 1;
        examine.setStatus(status);
        examine.setAdminId(adminId);
        examine.setFinishTime(new Date());

//        examineUpdate(examine);
        examineMapper.updateById(examine);

        if (examine.getType() == IExamineService.ABUSE)
            abuseService.handle(examine.getTargetId(), isPassed);
    }


    private Examine fill(long targetId, Date date){
        Examine exmaine = new Examine();
        exmaine.setCreateTime(date);
//        exmaine.setUpdateTime(date);
        exmaine.setTargetId(targetId);
        return exmaine;
    }

    @Deprecated
    private Examine examineSelectById(long examineId){
        Examine examine = null;
        try {
            examine = examineMapper.selectById(examineId);
        }catch (Exception e){
            e.printStackTrace();
            throw new DBException(DBException.EXAMINE_SELECT_FAILURE);
        }
        return examine;
    }

    @Deprecated
    private void examineInsert(Examine examine){
        try {
            examineMapper.insert(examine);
        }catch (Exception e){
            throw new DBException(DBException.EXAMINE_INSERT_FAILURE);
        }
    }

    @Deprecated
    private List<Examine> examineSelectPage(Page<Examine> page, EntityWrapper<Examine> ew){
        List<Examine> list = null;
        try {
            list = examineMapper.selectPage(page, ew);
        }catch (Exception e){
            throw new DBException(DBException.EXAMINE_SELECT_FAILURE);
        }
        return list;
    }

    @Deprecated
    private void examineUpdate(Examine examine){
        try {
            examineMapper.updateById(examine);
        }catch (Exception e){
            throw new DBException(DBException.EXAMINE_UPDATE_FAILURE);
        }
    }
}
